package com.msyy.tj_message.config;

import com.msyy.tj_message.entity.Menu;
import com.msyy.tj_message.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Classname SecurityInterceptor
 * @Description 拦截器
 * @Date 2020/5/23 8:42
 * @Created by You
 */
public class SecurityInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private IMenuService menuService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws IOException {
        HttpSession session = request.getSession();
        System.out.println("这是路径地址："+request.getRequestURI());

        //如果路径地址是/tj_message/、/tj_message/pages/index.jsp、/tj_message
        //去查询菜单列表
        if("/tj_message/pages/index.jsp".equals(request.getRequestURI()) || "/tj_message/".equals(request.getRequestURI()) || "/tj_message".equals(request.getRequestURI())){
            //根据用户角色权限查询菜单
            List<Menu> parentMenuList = menuService.getParentMenuByRoleId();
            List<Menu> childMenuList = menuService.getChildMenuByRoleId();
            //将用户所能看到的菜单存入到session中
            session.setAttribute("parentMenuList", parentMenuList);
            session.setAttribute("childMenuList", childMenuList);
            System.out.println("--------- " + parentMenuList);
            System.out.println("--------- " + childMenuList);
        }

        return  true;

    }
}
